package com.example.shop.fgo.task.upload.view;


import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.shop.fgo.R;
import com.example.shop.fgo.task.upload.model.SDEntity;
import com.example.shop.fgo.utils.IconUtil;

import java.util.ArrayList;
import java.util.List;

public class ItemLvSdScanAdapter extends BaseAdapter implements View.OnClickListener {

    private  List<SDEntity> mEntities;

    private Context mContext;
    private LayoutInflater layoutInflater;
    public  List<SDEntity> mEntitiesChecked=new ArrayList<>();
    public ItemLvSdScanAdapter(Context context, List<SDEntity> entities) {
        this.mContext = context;
        this.layoutInflater = LayoutInflater.from(context);
        this.mEntities=entities;
    }
    
    public void setEntities(List<SDEntity> entities) {
        mEntities = entities;
        notifyDataSetChanged();
    }
    
    @Override
    public int getCount() {
        return mEntities.size();
    }

    @Override
    public SDEntity getItem(int position) {
        return mEntities.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (convertView == null) {
            convertView = layoutInflater.inflate(R.layout.item_lv_sd_scan, parent,false);
            convertView.setTag(new ViewHolder(convertView));
        }
        initializeViews((SDEntity)getItem(position), (ViewHolder) convertView.getTag(),position);
        return convertView;
    }

    private void initializeViews(SDEntity entity, ViewHolder holder,int position) {
        
        holder.tvTitleItem.setText(entity.getName());

        
        if (entity.isDir()){
            holder.tvSizeItemSd.setVisibility(View.INVISIBLE);
            holder.imgvIconItemSd.setImageResource(R.drawable.cloud_dir_icon);
        }else {
    
            holder.imgvIconItemSd.setImageResource(IconUtil.getDrawable(entity.getIco()));
            holder.tvSizeItemSd.setVisibility(View.VISIBLE);
            holder.tvSizeItemSd.setText(entity.getSizeStr());
        }
        holder.cbItem.setTag(entity);
        holder.cbItem.setOnClickListener(this);
        holder.cbItem.setChecked(entity.isChecked());
    }
    
    @Override
    public void onClick(View v) {
        CheckBox checkBox= (CheckBox) v;
        SDEntity sdEntity= (SDEntity) v.getTag();
        if (checkBox.isChecked()){
            sdEntity.setChecked(true);
            mEntitiesChecked.add(sdEntity);
        }else {
            sdEntity.setChecked(false);
            mEntitiesChecked.remove(sdEntity);
        }
        
    }
    
    protected class ViewHolder {
        private ImageView imgvIconItemSd;
    private TextView tvTitleItem;
    private TextView tvSizeItemSd;
    private CheckBox cbItem;

        public ViewHolder(View view) {
            imgvIconItemSd = (ImageView) view.findViewById(R.id.imgv_icon_item_sd);
            tvTitleItem = (TextView) view.findViewById(R.id.tv_title_item);
            tvSizeItemSd = (TextView) view.findViewById(R.id.tv_size_item_sd);
            cbItem = (CheckBox) view.findViewById(R.id.cb_item);
        }
    }
}
